<template>
  <div class="login-wrap clearfix">
    <!-- 欢迎模块 -->
    <div class="welcome-box left">
      <h1>Welcome</h1>
      <div class="line"></div>
      <h4>好新优运营管理系统</h4>
      <p>—— 社交新零售电商平台 ——</p>
    </div>

    <el-form
      :model="loginForm"
      :rules="fieldRules"
      ref="loginForm"
      label-width="0px"
    >
      <!-- 登录模块 -->
      <div class="login-box left">
        <div class="logo">
          <img src="../assets/image/logo.png" alt="logo" />
        </div>

        <el-form-item prop="account">
          <el-input
            placeholder="账号"
            prefix-icon="el-icon-mobile-phone"
            v-model="loginForm.account"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            class="password"
            placeholder="密码"
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
            show-password
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>

        <div class="operation clearfix">
          <div class="right" @click="handleFindPassword">忘记密码</div>
        </div>

        <el-button type="primary" class="btn login-btn" @click.native.prevent="login">登录</el-button>
      </div>
    </el-form>
    <!-- 备案信息 -->
    <div class="company-info">
      <p>版权所有 Copyright © 2018-2020 XXX有限公司</p>
      <p>粤ICP备 XXXX号</p>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import Cookies from "js-cookie"
export default {
  data() {
    return {
      loading: false,
      loginForm: {
        account: "admin",
        password: "123456",
        captcha: "",
      },
      fieldRules: {
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
        // ,
        // captcha: [
        //   { required: true, message: '请输入验证码', trigger: 'blur' }
        // ]
      },
      checked: false
    };
  },

  created() {},

  methods: {
    // 找回密码
    handleFindPassword() {
      this.$router.push({ path: "/findPassword" });
    },

    // 获取管理员信息
    validateForm() {
      return this.$refs["loginForm"].validate(valid => {
        if (valid) {
          this.checked=true;
        }
        else
        {
          this.checked=false;

        }

      });

    },

    login() {
      this.validateForm()
      if (this.checked == false) {
        return;
      }


      let userInfo = {
        account: this.loginForm.account,
        password: this.loginForm.password,
        captcha: this.loginForm.captcha
      };
      this.$api.login
        .login(userInfo)
        .then(res => {
          if (res.msg != null) {
            this.$message({
              message: res.msg,
              type: "error"
            });
          } else {
            Cookies.set("token", res.data.token); // 放置token到Cookie
            sessionStorage.setItem("user", userInfo.account); // 保存用户到本地会话
            this.$store.commit("menuRouteLoaded", false); // 要求重新加载导航菜单
            this.$router.push({path:"/home"}); // 登录成功，跳转到主页
          }
          this.loading = false;
        })
        .catch(res => {
          this.$message({
            message: res.message,
            type: "error"
          });
        });
    }
  }
};
</script>

<style lang="stylus" scoped>
.login-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  text-align: center;
  background: url('../assets/image/login_bg.png') no-repeat center;

  .welcome-box {
    padding-top: 40px;
    width: 380px;
    height: 430px;
    text-align: center;
    color: #fff;
    background: url('../assets/image/welcome_bg.png') no-repeat center;

    h1 {
      font-size: 50px;
    }

    .line {
      margin: 16px auto 8px;
      width: 36px;
      height: 5px;
      background-color: #fff;
      border-radius: 5px;
    }

    h4 {
      font-size: 30px;
    }

    p {
      margin-top: 12px;
      font-size: 14px;
    }
  }

  .login-box {
    width: 410px;
    height: 470px;
    background-color: #fff;
    border-radius: 4px;

    .logo {
      padding: 45px 0;
      text-align: center;

      img {
        width: 100px;
      }
    }
    .el-form-item{
      display:inline-block;
      }
    .el-input {
      margin: 0 auto 0px;
      width: 244px;
      font-size: 14px;
      color: #999;
      border-bottom: 1px solid #ccc;
    }

    >>> .el-input__inner {
      font-size: 16px;
      border: none;
    }

    >>> .el-input__icon {
      width: 0;
      font-size: 16px;
    }

    .password {
      margin: 0 auto 0px;
    }

    .captcha-box {
      position: relative;
      margin: 0 auto;
      width: 244px;
      text-align: left;

      >>> .el-input__inner {
        width: 160px;
      }

      .el-button {
        position: absolute;
        right: 0;
        bottom: 24px;
        padding: 0;
        width: 90px;
        height: 28px;
        font-size: 12px;
        background-color: #3a88f7;
        border-color: #3a88f7;
        border-radius: 14px;
      }
    }

    .operation {
      margin: 0 auto 30px;
      width: 244px;

      .left {
        color: #3a88f7;
      }

      .right {
        color: #7f7f7f;
      }

      .left, .right {
        cursor: pointer;
      }
    }

    .operation, .el-checkbox {
      color: #999;
    }

    .btn {
      margin-bottom: 20px;
      width: 244px;
      height: 44px;
      text-align: center;
      font-size: 18px;
      border-radius: 4px;
    }

    .captcha-btn {
      width: 90px;
      height: 28px;
      line-height: 28px;
      text-align: center;
      font-size: 12px;
      color: #fff;
      background-color: #3a88f7;
      border-radius: 10px;
    }

    .login-btn, .edit-btn {
      color: #fff;
      background-color: #3a88f7;
    }

    .edit-btn {
      margin-top: 20px;
    }

    .apply-btn {
      color: #4c4c4c;
      background-color: #fff;
      border: 1px solid #999;
    }

    .el-button+.el-button {
      margin-left: 0;
    }
  }

  .company-info {
    position: absolute;
    bottom: 2%;
    line-height: 18px;
    text-align: center;
    font-size: 12px;
    color: #fff;
  }
}

.ie9 .login-box {
  margin: 5% auto 0;
}
</style>
